﻿using EFCore.Models;
using Microsoft.EntityFrameworkCore;
using SIService;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SService
{
    public class InventoryS : InventorySI
    {
        private readonly WmssystemContext myDbContext;

        public InventoryS(WmssystemContext myDbContext)
        {
            this.myDbContext = myDbContext;
        }
        public Task<List<sys_inventory>> FenYe(int pageSize, int currentPage, string code = null, int? result = null, int? type = null, int? wid = null)
        {
            var query = myDbContext.sys_inventory.AsQueryable();

            // 根据条件进行查询
            if (!string.IsNullOrEmpty(code))
            {
                query = query.Where(c => c.i_icode.Contains(code));
            }
            if (result.HasValue)
            {
                query = query.Where(c => c.i_iresult == result);
            }
            if (type.HasValue)
            {
                query = query.Where(c => c.i_itype == type);
            }
            if (wid.HasValue)
            {
                query = query.Where(c => c.i_iwid == wid);
            }
            query = query.Where(c => c.i_iisdelete == true);
            // 分页查询
            return query.Skip((currentPage - 1) * pageSize).Take(pageSize).ToListAsync();
        }
        //添加
        public Task<int> Add(sys_inventory add)
        {
            myDbContext.sys_inventory.Add(add);
            return myDbContext.SaveChangesAsync();
        }
        //逻辑删除
        public async Task<int> UpdIsDelete(int id, bool isDeleted)
        {
            var entity = await myDbContext.sys_inventory.FindAsync(id);
            // 更新实体的删除状态
            entity.i_iisdelete = isDeleted;
            // 标记实体为已修改状态
            myDbContext.Entry(entity).State = EntityState.Modified;
            // 保存更改到数据库
            return await myDbContext.SaveChangesAsync();
        }
        public async Task<int> RkShen(sys_inventory shen, string to)
        {
            // 1. 根据指定条件查找要审核的记录
            var purchaseOrder = await myDbContext.sys_inventory.FindAsync(shen.i_iid);

            if (purchaseOrder == null)
            {
                // 如果没有找到对应的记录，可以进行适当的处理，比如抛出异常或者返回错误码
                throw new Exception("指定的记录不存在");
            }
            if (to == "通过")
            {
                purchaseOrder.i_ioutbound_status = 1;
                purchaseOrder.i_ientry_status = 1;
                purchaseOrder.i_ishen = 2;
                //把盘点的数量回写到入库明细表中
                var ppList = await myDbContext.sys_inventory_details.Where(c => c.i_icode == purchaseOrder.i_icode).ToListAsync();
                foreach (var pp in ppList)
                {
                    var ccList = await myDbContext.PO_Inventorydetails.Where(c => c.I_IProductCode == pp.id_goods_code).ToListAsync();
                    foreach (var cc in ccList)
                    {
                        
                            cc.I_IQuantity = (int?)(cc.I_IQuantity + pp.id_profit_and_loss_quantity);
                            // 如果需要更新其他属性，也可以在这里进行修改
          

                    }
                }
            }
            else
            {
                purchaseOrder.i_ishen = 3;
            }
            // 2. 更新找到的记录的相应字段
            purchaseOrder.i_iupdateuser = 1; // 以此类推，更新需要修改的字段
            purchaseOrder.i_iupdatetime = DateTime.Now;
            //purchaseOrder.P_Reviewcomments = shen.P_Reviewcomments;


            // 3. 将更新后的记录保存回数据库中
            myDbContext.sys_inventory.Update(purchaseOrder);
            return await myDbContext.SaveChangesAsync(); // 返回受影响的行数
        }
        public async Task<int> UpdShen(int id, int shen)
        {
            var entity = await myDbContext.sys_inventory.FindAsync(id);
            // 更新实体的删除状态
            entity.i_ishen = shen;
            // 标记实体为已修改状态
            myDbContext.Entry(entity).State = EntityState.Modified;
            // 保存更改到数据库
            return await myDbContext.SaveChangesAsync();
        }
        public async Task<int> Upd(sys_inventory sys_inventory)
        {
            var existingOrder = await myDbContext.sys_inventory.FirstOrDefaultAsync(o => o.i_iid == sys_inventory.i_iid);
            // 检查订单是否存在
            if (existingOrder == null)
            {
                return 0; // 表示未找到要更新的订单
            }
            existingOrder.i_icode = sys_inventory.i_icode;
            existingOrder.i_iwid = sys_inventory.i_iwid;
            existingOrder.i_itype = sys_inventory.i_itype;
            existingOrder.i_istart_time = sys_inventory.i_istart_time;
            existingOrder.i_iend_time = sys_inventory.i_iend_time;
            existingOrder.i_ioperator = sys_inventory.i_ioperator;
            existingOrder.i_iremark = sys_inventory.i_iremark;

            return await myDbContext.SaveChangesAsync();
        }
    }
}
